package com.study.fetch;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader.Feature;
import com.alibaba.fastjson2.TypeReference;
import com.study.fetch.JygsDemo.JysdDTO.Commimu;
import com.study.fetch.JygsDemo.JysdDTO.Resp;
import com.study.fetch.JygsDemo.JysdDTO.Result;
import lombok.Data;
import lombok.experimental.Accessors;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author zhuzi
 * 2024/3/31 17:46
 */
public class JygsDemo {

    @Test
    public void uuid() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid.toString());
        System.out.println(UUID.fastUUID());

        System.out.println(IdUtil.getSnowflakeNextId());
        System.out.println();
    }

    @Test
    public void fetch() {
        String fastSimpleUUID = IdUtil.fastSimpleUUID();
        System.out.println(fastSimpleUUID);
        Map<String, String> headerMap = MapUtil.<String, String>builder()//
                .put("Timestamp", System.currentTimeMillis() + "")//
                .put("Token", fastSimpleUUID)//
                .put("Platform", "2").build();
        String reqBody = StrUtil.format("{\"category_id\":\"\",\"limit\":{},\"order\":1,\"start\":{},\"type\":0,\"back_garden\":0}", 150, 1);
        String url = "https://app.jiuyangongshe.com/jystock-app/api/v2/article/community";
        String body = HttpRequest.post(url).headerMap(headerMap, true).body(reqBody).timeout(5000).execute().body();

        System.out.println(body);
        Resp<Commimu> resp = JSON.parseObject(body, new TypeReference<Resp<Commimu>>() {
        }, Feature.SupportSmartMatch);
        for (Result result : resp.getData().getResult()) {
            System.out.println(result);
        }

    }


    public interface JysdDTO {
        @Data
        @Accessors(chain = true)
        class Resp<T> {
            private String msg;
            private T data;
            private String errCode;
            private long serverTime;
        }

        /**
         * 社群
         */
        @Data
        @Accessors(chain = true)
        class Commimu {
            private int pageNo;
            private int pageSize;
            private String orderBy;
            private String order;
            private boolean autoCount;
            private Map map;
            private String params;
            private List<Result> result;
            private int totalCount;
            private int first;
            private int totalPages;
            private boolean hasNext;
            private int nextPage;
            private boolean hasPre;
            private int prePage;
        }

        @Data
        @Accessors(chain = true)
        class Result {
            private String articleId;
            private int isTop;
            private String userId;
            private int isLike;
            private List<String> categoryIdSet;
            private String sourceId;
            private String title;
            private String subtitle;
            private String image;
            private String cover;
            private String url;
            private int type;
            private int readLimit;
            private int commentCount;
            private int collectCount;
            private int likeCount;
            private int stepCount;
            private int forwardCount;
            private double integral;
            private int isUserTop;
            private int readIntegral;
            private String readLimitTime;
            private int fansLimit;
            private int copyLimit;
            private int oldType;
            private int hide;
            private LocalDateTime createTime;
            private int isMakeWordCloud;
            private LocalDateTime syncTime;
            private int isFlatter;
            private String featureImg;
            private int interestDisclosure;
            private LocalDateTime newInteractionTime;
            private String sensitiveWords;
            private String content;
            private User user;
            private List<Stock> stockList;
        }

        class Stock {
            private String stockId;
            private String name;
            private String code;
        }

        class User {
            private int followType;
            private String investmentStyleId;
            private String userId;
            private String nickname;
            private int followStatus;
            private String factionId;
            private String avatar;
            private int medalCount;
            private String styleStr;
        }
    }
}
